package com.info33.platform.business.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.business.app.dto.AppMenuRoleDTO;
import com.info33.platform.business.app.dto.AppRoleDTO;
import com.info33.platform.business.app.entity.AppMenuRole;
import com.info33.platform.business.app.service.AppMenuRoleService;
import com.info33.platform.business.app.vo.AppMenuVO;
import com.info33.platform.business.app.vo.AppRoleVO;
import com.info33.platform.business.app.vo.AppTenantUserVO;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dto.SysUserDTO;
import com.info33.platform.system.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/AppMenuRole")
@Api(tags = "app - 菜单 - 用户/角色/全部")
public class AppMenuRoleController extends BaseController<AppMenuRoleService, AppMenuRole> {

    private final AppMenuRoleService appMenuRoleService;

    @Autowired
    public AppMenuRoleController(AppMenuRoleService appMenuRoleService) {
        super(appMenuRoleService);
        this.appMenuRoleService = appMenuRoleService;
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * save
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/pc/bind/role", name = "只是新增，单角色新增")
    @ApiOperation(value = "pc - 菜单绑定角色")
    public Result<Object> pcBindRole(
            @RequestBody AppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return appMenuRoleService.pcBindRole(dto);
    }

    @PostMapping(value = "/pc/bind/user")
    @ApiOperation(value = "pc - 菜单绑定用户", notes = "只是新增，多用户新增")
    public Result<Object> pcBindUser(
            @RequestBody AppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return appMenuRoleService.pcBindUser(dto);
    }

    @PostMapping(value = "/pc/bind/all")
    @ApiOperation(value = "pc - 菜单绑定全部", notes = "只是新增，仅需要传入菜单 id 集合")
    public Result<Object> pcBindAll(
            @RequestBody AppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return appMenuRoleService.pcBindAll(dto);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * list（全部菜单没有 list api）
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/pc/user/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 拥有权限的用户", notes = "入参说明<br/>" +
            "trueName：模糊查询<br/>" +
            "mobile：模糊查询")
    public Result<IPage<SysUserVO>> pcUser(
            @PathVariable int pageNum,
            @PathVariable int pageSize,
            @RequestBody SysUserDTO dto) {
        dto.setTenantId(getTenantId());
        return Result.ok(appMenuRoleService.pcUser(new Page<>(pageNum, pageSize), dto));
    }

    @PostMapping(value = "/pc/role/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 拥有权限的角色", notes = "入参说明<br/>" +
            "name：模糊查询")
    public Result<IPage<AppRoleVO>> pcRole(
            @PathVariable int pageNum,
            @PathVariable int pageSize,
            @RequestBody AppRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return Result.ok(appMenuRoleService.pcRole(new Page<>(pageNum, pageSize), dto));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * detail
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @GetMapping(value = "/pc/user/{uid}")
    @ApiOperation(value = "pc - 拥有权限的某用户详情", notes = "R 租户用户 id")
    public Result<AppTenantUserVO> pcUserDetail(
            @PathVariable(value = "uid") Long uid) {
        return Result.ok(appMenuRoleService.pcUserDetail(uid));
    }

    @GetMapping(value = "/pc/role/{rid}")
    @ApiOperation(value = "pc - 拥有权限的某角色详情", notes = "R 查询租户角色 id")
    public Result<AppRoleVO> pcRoleDetail(
            @PathVariable(value = "rid") Long rid) {
        return Result.ok(appMenuRoleService.pcRoleDetail(rid));
    }

    @GetMapping(value = "/pc/all")
    @ApiOperation(value = "pc - 绑定全部的菜单 id 集合", notes = "R 查询")
    public Result<List<AppMenuVO>> pcAll() {
        return Result.ok(appMenuRoleService.pcAll(getTenantId()));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * update
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PutMapping(value = "/pc/edit/user")
    @ApiOperation(value = "pc - 编辑用户")
    public Result<Object> pcEditUser(
            @RequestBody AppMenuRoleDTO dto) {
        return appMenuRoleService.pcEditUser(dto);
    }

    @PutMapping(value = "/pc/edit/role")
    @ApiOperation(value = "pc - 编辑角色")
    public Result<Object> pcEditRole(
            @RequestBody AppMenuRoleDTO dto) {
        return appMenuRoleService.pcEditRole(dto);
    }

    @PutMapping(value = "/pc/edit/all")
    @ApiOperation(value = "pc - 编辑全部")
    public Result<Object> pcEditAll(
            @RequestBody AppMenuRoleDTO dto) {
        dto.setTenantId(getTenantId());
        return appMenuRoleService.pcEditAll(dto);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * remove from db
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @DeleteMapping(value = "/pc/remove/user/{uid}")
    @ApiOperation(value = "pc - 删除某用户所有菜单", notes = "后期优化：避免全盘扫描，根据 id 删除")
    public Result<Object> pcRemoveUser(
            @PathVariable(value = "uid") Long uid) {
        return appMenuRoleService.remove(new QueryWrapper<AppMenuRole>()
                .eq("user_id", uid)
                .eq("type", 2))
                ? Result.ok() : Result.error();
    }

    @DeleteMapping(value = "/pc/remove/role/{rid}")
    @ApiOperation(value = "pc - 删除某角色所有菜单", notes = "后期优化：避免全盘扫描，根据 id 删除")
    public Result<Object> pcRemoveRole(
            @PathVariable(value = "rid") Long rid) {
        return appMenuRoleService.remove(new QueryWrapper<AppMenuRole>()
                .eq("role_id", rid)
                .eq("type", 1))
                ? Result.ok() : Result.error();
    }

    @DeleteMapping(value = "/pc/remove/all")
    @ApiOperation(value = "pc - 删除所有政权为全部的菜单", notes = "后期优化：避免全盘扫描，根据 id 删除")
    public Result<Object> pcRemoveAll() {
        return appMenuRoleService.remove(new QueryWrapper<AppMenuRole>()
                .eq("type", 3)
                .eq(TableConstant.TENANT_ID, getTenantId()))
                ? Result.ok() : Result.error();
    }

}